package com.up.upzl.biz.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.up.upzl.biz.domain.ModuleArticle;
import com.up.upzl.biz.domain.Module;
import com.up.upzl.biz.domain.Module.PageType;
import com.up.upzl.biz.service.ModuleArticleService;
import com.up.upzl.biz.service.ModuleService;



/**
 * 模块信息管理
 * 
 * @author youke
 *
 */

@Controller
@RequestMapping("moduleArticle")
public class ModuleArticleController {
	
	/**
	 * 社区子集信息查看
	 * 
	 * @author youke
	 *
	 */
	
	@Autowired
	private ModuleArticleService moduleArticleService;
	@Autowired
	private ModuleService moduleService;
	
	@GetMapping("communityList")
	public String List(PageType pageType, Long moduleId, String title, Pageable page, Model model) {
		Module module = moduleService.findOne(moduleId);
		if(module.getPageType() != Module.PageType.社区) {
			return "error/500";
		}
		model.addAttribute("module", module);
		
		pageType = Module.PageType.社区;
		page = new PageRequest(page.getPageNumber(), page.getPageSize(), new Sort(Direction.DESC, "sort"));
		Page<ModuleArticle> list = moduleArticleService.findByCondition(pageType, moduleId, title, page);
		model.addAttribute("pageList", list);
		
		return "moduleArticle/list";
	}
	
	/**
	 * 首页子集信息查看
	 * 
	 * @author youke
	 *
	 */
	@GetMapping("pageList")
	public String PageList(PageType pageType, Long moduleId, String title, Pageable page, Model model) {
		Module module = moduleService.findOne(moduleId);
		if(module.getPageType() != Module.PageType.首页) {
			return "error/500";
		}
		model.addAttribute("module", module);
		
		pageType = Module.PageType.首页;
		page = new PageRequest(page.getPageNumber(), page.getPageSize(), new Sort(Direction.DESC, "sort"));
		Page<ModuleArticle> list = moduleArticleService.findByCondition(pageType, moduleId, title, page);
		model.addAttribute("pageList", list);
		
		return "moduleArticle/pageList";
	}
	
	/**
	 * 跳转添加社区模块信息页面
	 * 
	 * @param model
	 * @return
	 */
	@GetMapping("addPage")
	public String add(Long moduleId, Model model) {
		Module module = moduleService.findOne(moduleId);
		if(module.getPageType() != Module.PageType.社区) {
			return "error/500";
		}
		model.addAttribute("module", module);
		return "moduleArticle/add";
	}
	

	/**
	 * 跳转添加首页模块信息页面
	 * 
	 * @param model
	 * @return
	 */
	@GetMapping("homeAddPage")
	public String addHome(Long moduleId, Model model) {
		Module module = moduleService.findOne(moduleId);
		if(module.getPageType() != Module.PageType.首页) {
			return "error/500";
		}
		model.addAttribute("module", module);
		return "moduleArticle/pageAdd";
	}
	
	/**
	 * 保存对象
	 */
	@PostMapping({"add", "homeAdd"})
	public @ResponseBody ModuleArticle save(ModuleArticle moduleArticle) {
		return moduleArticleService.save(moduleArticle);
	}

	/**
	 * 跳转到社区更新页面
	 */
	@GetMapping({ "updatePage", "detail"})
	public String update(Long id, Model model) {
		ModuleArticle moduleArticle = moduleArticleService.findOne(id);
		model.addAttribute("entity", moduleArticle);
		
		
		return "moduleArticle/update";
	}
	
	/**
	 * 跳转到首页更新页面
	 */
	@GetMapping({ "homeUpdatePage", "homeDetail"})
	public String updateHome(Long id, Model model) {
		ModuleArticle moduleArticle = moduleArticleService.findOne(id);
		model.addAttribute("entity", moduleArticle);
		
		
		return "moduleArticle/pageUpdate";
	}

	/**
	 * 更新
	 */
	@PostMapping({"update", "homeUpdate"})
	public @ResponseBody ModuleArticle update(ModuleArticle moduleArticle) {
		Assert.notNull(moduleArticle.getId(), "Id can`t be null");
		return moduleArticleService.save(moduleArticle);
	}

	/**
	 * 删除
	 */
	@PostMapping({"delete", "homeDelete"})
	public @ResponseBody Map<String, String> delete(ModuleArticle moduleArticle) {
		Assert.notNull(moduleArticle.getId(), "Id can`t be null");
		Map<String, String> map = new HashMap<>();
		try {
			moduleArticleService.delete(moduleArticle.getId());
		} catch (Exception e) {
			map.put("Type", "500");
			return map;
		}
		map.put("Type", "200");
		return map;
	}

}
